// views/txtdetail/index.js
var time = require('../../utils/util');
var BASE_URL = getApp().globalData.BASE_URL
Page({
  // , {
  //   "pagePath": "pages/question/index",
  //   "iconPath": "./images/Index_index/jia.png",
  //   "selectedIconPath": "./images/Index_index/jia.png"
  // }

  /**
   * 页面的初始数据
   */
  data: {
    inputshow: false,
    id: '',
    flag: true,
    article: '',
    showloading: true,
    value: '',
    imgsrc: '',
    good: false,
    content: '',
    num: '',
    pinglunlist: '',
    ifopenhuifu: '',
    ifopen: false,
    huifu_input: false,
    placeHolder: '',
    iid: '',

    zongprice: '',
    email: '',
    showemailpop: false,
    showbotinput: true,
    fileid:'',
    mobile:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // options.id 就是首页传过来的id，接下来循环找到id所匹配的数据就可以进行渲染啦！
    this.setData({
      id: options.id
      // id: 11859
    })
    console.log(this.data.id)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.articledetail()
  },
  articledetail: function () {
    var _this = this;
    var userinfo = wx.getStorageSync("USER")
    wx.request({
      url: `${BASE_URL}/ajax/wenzhangxiangqing?id=${_this.data.id}`,
      method: "GET",
      header: {
        'Content-Type': 'application/json' //GET方式是这个
      },
      success(res) {
        res.data.other_info = ''
        res.data.time = time.formatTimeTwo(res.data.time, 'Y-M-D h:m');
        if (res.data.good !== '') {
          res.data.good = res.data.good.split(",")
          if (res.data.good.includes('' + (userinfo.id))) {
            _this.setData({
              good: true
            })
          } else {
            _this.setData({
              good: false
            })
          }
        } else {
          _this.setData({
            good: false
          })
        }
        if (res.data.file_arr !== '') {
          var arr = []
          res.data.file_arr.forEach(item => {
            item.size = (item.size / 1024 / 1024).toFixed(2)
            arr.push(item.amount)
          })
          let price = arr.reduce(function (total, value) {
            return total + value
          })
          _this.setData({
            zongprice: price
          })
        }
        console.log(res)
        _this.setData({
          article: res.data,
          showloading: false,
          num: res.data.good.length,
        })
      }
    })



    wx.request({
      url: `${BASE_URL}/ajax/xcxpinglunlist`,
      method: "GET",
      data: {
        id: _this.data.id
      },
      header: {
        'Content-Type': 'application/json' //GET方式是这个
      },
      success(res) {
        console.log(res)
        res.data.forEach(item => {
          item.time = time.formatTimeTwo(item.time, 'Y-M-D h:m');
          if (item.avatar.length == 37) {
            item.avatar = `${BASE_URL}` + item.avatar
          }
          item.child.forEach(function (item, index) {
            item.time = time.formatTimeTwo(item.time, 'Y-M-D h:m:s');
            item.child.forEach(function (item, index) {
              item.time = time.formatTimeTwo(item.time, 'Y-M-D h:m:s');
            })
          })

        })
        _this.setData({
          pinglunlist: res.data
        })
      }
    })
  },

  //打开邮箱填写
  openemailpop: function (e) {
      var email = wx.getStorageSync("email")
      if(email!==''){
        this.setData({
          email:email,
        })
      }
   
    this.setData({
      showemailpop: true,
      showbotinput: false,
      fileid:e.currentTarget.dataset.fileid
    })
  },
  //关闭tianxie
  onClose() {
    this.setData({
      showemailpop: false,
      showbotinput: true
    });
  },
  //填写完成

  emailsendbtn: function () {
    var _this=this
    var userinfo = wx.getStorageSync("USER")
    var mobile = wx.getStorageSync("phoneNumber")

    if (this.data.email == '') {
      wx.showToast({
        title: '邮箱未填写',
        icon: 'none',
        duration: 2000
      })
    } else {
      wx.request({
        url: `${BASE_URL}/publish/pay`,
        method: "POST",
        data: {
          pid: _this.data.id,
          id: _this.data.fileid,
          type:'xiaochengxv',
          uid:userinfo.id,
          email:_this.data.email,
          username:userinfo.username,
          mobile:mobile?mobile:''
        },
        header: {
          'Content-Type': 'application/json' //GET方式是这个
        },
        success(res) {
          console.log(res)
          wx.setStorage({
            key: "email",
            data: _this.data.email
          })
          if(res.data.text=='已发送到邮箱'){
            wx.showToast({
              title: '文件稍后会发送到您的邮箱，请注意查收。',
              icon: 'none',
              duration: 2000
            })
          }else{
            wx.navigateTo({
              url: `../paypage/index?id=${res.data.id}`,
            })
          }
        }
      })
   
      _this.setData({
        showemailpop: false,
        showbotinput: true
      });
    }
  },














  openinput: function () {
    this.setData({
      inputshow: true
    })
  },
  close_focus: function () {
    this.setData({
      inputshow: false,
      huifu_input: false
    })
  },
  //回复评论
  openhuifu: function (e) {
    this.setData({
      ifopenhuifu: e.currentTarget.dataset.ifopenhuifu,
      ifopen: !this.data.ifopen
    })
  },
  opinput_huifu_input: function (e) {
    console.log(11)
    this.setData({
      huifu_input: true,
      placeHolder: e.currentTarget.dataset.name,
      iid: e.currentTarget.dataset.iid,
      ifopen: !this.data.ifopen
    })
  },

















  gogood: function () {
    var _this = this;
    var userinfo = wx.getStorageSync("USER")
    console.log(userinfo)
    wx.request({
      url: `${BASE_URL}/publish/good`,
      method: "GET",
      data: {
        id: _this.data.id,
        uid: userinfo.id
      },
      header: {
        'Content-Type': 'application/json' //GET方式是这个
      },
      success(res) {
        console.log(res)
        _this.setData({
          num: res.data.num
        })
        if (res.data.zt == 2) {
          _this.setData({
            good: false
          })
        } else {
          _this.setData({
            good: true
          })
        }
      }
    })
  },
  gopinglun: function () {
    var _this = this;
    if (_this.data.value !== '') {
      var userinfo = wx.getStorageSync("USER")
      console.log(userinfo)
      wx.request({
        url: `${BASE_URL}/ajax/xcxpinglun`,
        method: "POST",
        data: {
          id: _this.data.id,
          uid: userinfo.id,
          username: userinfo.username,
          content: _this.data.value
        },
        header: {
          'Content-Type': 'application/json' //GET方式是这个
        },
        success(res) {
          console.log(res)
          _this.articledetail()
          _this.setData({
            value: ''
          })
        }
      })
    } else {
      wx.showToast({
        title: '输入内容为空',
        icon: 'none',
        duration: 500
      })
    }

  },

  //回复评论
  gohuifupinglun: function () {
    var _this = this;
    if (_this.data.value !== '') {
      var userinfo = wx.getStorageSync("USER")
      console.log(userinfo)
      wx.request({
        url: `${BASE_URL}/publish/pl_huifu`,
        method: "POST",
        data: {
          pid: _this.data.id,
          uid: userinfo.id,
          username: userinfo.username,
          content: _this.data.value,
          avatar: userinfo.avatar,
          iid: _this.data.iid
        },
        header: {
          'Content-Type': 'application/json' //GET方式是这个
        },
        success(res) {
          console.log(res)
          _this.articledetail()
          _this.setData({
            value: ''
          })
        }
      })
    } else {
      wx.showToast({
        title: '输入内容为空',
        icon: 'none',
        duration: 500
      })
    }


  },







































  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (time.iflogin()) {
      var userinfo = wx.getStorageSync("USER")
      console.log(userinfo)
      this.setData({
        imgsrc: userinfo.avatar,
        flag: false,
      })
      if(userinfo.email!==''){
        this.setData({
          email: userinfo.email,
        })
        wx.setStorage({
          key: "email",
          data: userinfo.email
        })
      }
    }
  
  },
  toLogin: function () {
    wx.navigateTo({
      url: `/views/login/index`
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})